package com.xmall.product.controller;

import com.xmall.aspect.OperateRecord;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.product.SkuInfoDto;
import com.xmall.define.dto.product.SkuQuantity;
import com.xmall.product.define.request.spu.SkuQueryParams;
import com.xmall.product.service.ISkuInfoService;
import com.xmall.define.request.product.StockLockRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品sku信息 前端控制器
 * </p>
 *
 * @author xmall
 * @since 2023-11-02
 */
@RestController
@RequestMapping("/service/product/sku")
public class SkuInfoController {

    @Resource
    private ISkuInfoService skuInfoService;

    /**
     * 根据skuID列表查询sku列表（包含属性）
     */
    @PostMapping("/list")
    public Result<List<SkuInfoDto>> list(@RequestBody List<Long> skuIds) {
        return Result.success(skuInfoService.listWithCascade(new SkuQueryParams().setSkuIds(skuIds)));
    }

    /**
     * 库存检查（返回不通过的skuId列表）
     */
    @PostMapping("/stockCheck")
    public Result<List<Long>> stockCheck(@RequestBody List<SkuQuantity> skuQuantityList) {
        return Result.success(skuInfoService.stockCheck(skuQuantityList));
    }

    /**
     * 库存锁定
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @PostMapping("/stockLock")
    public Result stockLock(@Validated @RequestBody StockLockRequest request) {
        skuInfoService.stockLock(request);
        return Result.success();
    }
}
